package com.njupt.simulation.utils;



public class CalculateAER {
    // 地球平均半径（单位：千米）
    private static final double EARTH_RADIUS = 6371.0;

    // 计算两点之间的直线距离（单位：千米）
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return EARTH_RADIUS * c;
    }

    // 计算方位角（单位：角度）
    public static double calculateAzimuth(double lat1, double lon1, double lat2, double lon2) {
        double dLon = Math.toRadians(lon2 - lon1);
        double y = Math.sin(dLon) * Math.cos(Math.toRadians(lat2));
        double x = Math.cos(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) -
                Math.sin(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(dLon);
        double AZ = Math.toDegrees(Math.atan2(y,x));

        return AZ;
    }

    // 计算俯仰角（单位：角度）
    public static double calculateElevation(double lat1, double lon1, double alt1, double lat2, double lon2, double alt2) {
        double RE = 6378.0;
        /*double A = Math.cos(Math.toRadians(lat1))*Math.cos(Math.toRadians(lon1-lon2));
        double EL = Math.atan((A-RE/(RE+alt2)) /Math.pow(1-Math.pow(A,2),0.5));
        return Math.toDegrees(EL);*/
        double cosr = Math.cos(Math.toRadians(lat1))*Math.cos(Math.toRadians(lat2))*Math.cos(Math.toRadians(lon2-lon1))+Math.sin(Math.toRadians(lat1))*Math.sin(Math.toRadians(lat2));
        double EL = Math.acos(Math.sin(Math.acos(cosr))/Math.pow((1+Math.pow(RE/(alt2+RE),2)-2*(RE/(alt2+RE))*cosr),0.5));
        /*return Math.toDegrees(EL);*/
        return cosr;
    }

    /**
     *
     * @param coordinates1 地面站经纬高
     * @param coordinates2 卫星经纬高
     * @return 俯仰角
     */
    public static double calculateEL(double[] coordinates1, double[] coordinates2) {
        double[] Object1 = CoordinateTransform.coordinatetransform(coordinates1[0], coordinates1[1], coordinates1[2]);
        double[] Object2 = CoordinateTransform.coordinatetransform(coordinates2[0], coordinates2[1], coordinates2[2]);

        //获取对象1转换之后的坐标
        double x1 = Object1[0];
        double y1 = Object1[1];
        double z1 = Object1[2];

        //获取对象2转换之后的坐标
        double x2 = Object2[0];
        double y2 = Object2[1];
        double z2 = Object2[2];

        double x3 = x2-x1;
        double y3 = y2-y1;
        double z3 = z2-z1;


        double vector = x1*x3+y1*y3+z1*z3;
        double mold1 = Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)+Math.pow(z1,2));
        double mold3 = Math.sqrt(Math.pow(x3,2)+Math.pow(y3,2)+Math.pow(z3,2));

        double cosEL = vector/(mold1*mold3);
        double El = Math.acos(cosEL);
        return Math.toDegrees(El);

    }

    public static double calculateEL1(double[] coordinates1, double[] coordinates2) {

        //获取对象1转换之后的坐标
        double x1 = coordinates1[0];
        double y1 = coordinates1[1];
        double z1 = coordinates1[2];

        //获取对象2转换之后的坐标
        double x2 = coordinates2[0];
        double y2 = coordinates2[1];
        double z2 = coordinates2[2];

        double x3 = x2-x1;
        double y3 = y2-y1;
        double z3 = z2-z1;

        double vector = x1*x3+y1*y3+z1*z3;
        double mold1 = Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)+Math.pow(z1,2));
        double mold3 = Math.sqrt(Math.pow(x3,2)+Math.pow(y3,2)+Math.pow(z3,2));

        double cosEL = vector/(mold1*mold3);
        double El = Math.acos(cosEL);
        return Math.toDegrees(El);

    }

    public static void main(String[] args) {
        // 雷达站位置
        double stationLat = 27.0386; // 雷达站纬度
        double stationLon = 119.597; // 雷达站经度
        double stationAlt = 0.0288261; // 雷达站高度（单位：千米）

        // 卫星位置
        double satelliteLat = 27.814; // 卫星纬度（例如，纽约）
        double satelliteLon = 118.256; // 卫星经度（例如，纽约）
        double satelliteAlt = 554.627401; // 卫星高度（单位：千米）

        // 计算方位角和俯仰角
        double azimuth = calculateAzimuth(stationLat, stationLon, satelliteLat, satelliteLon);
        double elevation = calculateElevation(stationLat, stationLon, stationAlt, satelliteLat, satelliteLon, satelliteAlt);

        double []station = {118.256,27.814,0.02};
        double []satellite = {118.256,27.814,500};

        double[] leossttellite={-2660.49,5304.152,3478.007};
        double[] geostation={-2659.8637,4901.9194,3084.2878};
        double el1 = 90 - calculateEL1(geostation,leossttellite);
        System.out.println(Math.sin(Math.toRadians(21.31))*(562 / Math.sin(Math.toRadians(96.99))));

        System.out.println("俯仰角（单位：度）: " + el1);
        double EL = calculateEL(station,satellite);
        double distance = calculateDistance(28,117.5,28.3,117);
        System.out.println(distance);
        //System.out.println("俯仰角（单位：度）: " + EL);
    }
}